﻿//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/

/**
Annot页面鼠标事件响应类
*/



define(function (require, exports, module) {

    var IReader_MousePtHandler = FXREADER.Event.IReader_MousePtHandler;
    var Interface = FXREADER.Interface;
    var CAnnot_MousePtHandler = function (pAnnotPlugin) {
        //      CAnnot_PageEventHandler.superclass.constructor.call(this);
        this.m_pAnnotPlugin = pAnnotPlugin;
        this.m_nFocusAnnot = { nPage: -1, nAnnotIndex: -1 };
        this.m_bMoveDown = false;
        this.m_bOldPosY = 0;
        this.m_bOldPosX = 0;
    };
    // Interface.extend(CAnnot_PageEventHandler, IReader_PageEventHandler);

    CAnnot_MousePtHandler.prototype.GetType = function () {
        return "Annot";
    }

    CAnnot_MousePtHandler.prototype.GetDirectPopupParent = function (node) {
        var tmpNode = node;
        var regex_popup_annot = new RegExp("(^|\\s)" + "fwr_popup_annot" + "(\\s|$)");
        while (tmpNode != null && !regex_popup_annot.test(tmpNode.className)) {

            tmpNode = tmpNode.parentNode;
            if (/^body|html$/i.test(tmpNode.nodeName)) {
                // we ended up too high in the document structure. Quit!
                return null;
            }
        }
        return tmpNode;
    }

    CAnnot_MousePtHandler.prototype.OnLButtonDown = function (event) {
        var regex = new RegExp("(^|\\s)" + "fwr_popup_annot" + "(\\s|$)");
        var regex_popup_header = new RegExp("(^|\\s)" + "popup_header" + "(\\s|$)");
        var bPopup = regex.test(event.target.className);
        var bPopupHeader = regex_popup_header.test(event.target.className);
        var target = event.target;
        if (bPopupHeader)
            target = this.GetDirectPopupParent(event.target);
        if (target == null)
            return true;
        if (bPopup || bPopupHeader) {
            FXREADER.Common.PreventDefaults(event);
            var pageindex = parseInt(target.getAttribute("page-index"));
            var annot_index = parseInt(target.getAttribute("annot-index"));
            if (annot_index != this.m_nFocusAnnot.nAnnotIndex || this.m_nFocusAnnot.nPage != pageindex) {
                if (this.m_nFocusAnnot.nAnnotIndex != -1 && this.m_nFocusAnnot.nPage != -1) {
                    var pDocView = this.m_pAnnotPlugin.GetDocView();
                    var pPageView = pDocView.GetPageView(this.m_nFocusAnnot.nPage);
                    var jqPopup = $("#" + this.m_pAnnotPlugin.GetAnnotPopupDivID(pPageView, this.m_nFocusAnnot.nAnnotIndex));
                    if (jqPopup != null) {
                        jqPopup.css({ opacity: '0.8', filter: 'alpha(opacity=80)', zIndex: '3' });
                    }
                }
                $(target).css({ opacity: '1', filter: 'alpha(opacity=100)', zIndex: '1000' });
            }
            this.m_nFocusAnnot.nPage = pageindex;
            this.m_nFocusAnnot.nAnnotIndex = annot_index;
            this.m_bMoveDown = true;
            this.m_bOldPosY = event.pageY; //当前鼠标位置
            this.m_bOldPosX = event.pageX; //当前鼠标位置
            return true;
        }
        else {
            var regex_popup_child = new RegExp("(^|\\s)" + "popup_child" + "(\\s|$)");
            var bPopupChild = regex_popup_child.test(event.target.className);
            if (bPopupChild) {
                return true;  //如果不return true,刚好鼠标点在annot对应元素的滚动条上，会触发页面滚动事件
            }
        }

        return false;
    };
    CAnnot_MousePtHandler.prototype.OnLButtonUp = function (event) {
        this.m_bMoveDown = false;
        this.m_pAnnotPlugin.OnDraw();
        return false;
    };
    CAnnot_MousePtHandler.prototype.OnLButtonDblClk = function (event) {
        return false;
    };
    CAnnot_MousePtHandler.prototype.OnMouseMove = function (event) {
        if (this.m_bMoveDown) {
            this.m_pAnnotPlugin.ClearCavans();
            this.m_pAnnotPlugin.MovePopupAnnot(this.m_nFocusAnnot.nPage, this.m_nFocusAnnot.nAnnotIndex, event.pageX - this.m_bOldPosX, event.pageY - this.m_bOldPosY);
            this.m_bOldPosY = event.pageY; //当前鼠标位置
            this.m_bOldPosX = event.pageX; //当前鼠标位置
            return true;
        }

        return false;
    };
    CAnnot_MousePtHandler.prototype.OnRButtonDown = function (event) {
        return false;
    };
    CAnnot_MousePtHandler.prototype.OnRButtonUp = function (event) {
        return false;
    };

    CAnnot_MousePtHandler.prototype.OnRButtonDblClk = function (event) {
        return false;
    };
    CAnnot_MousePtHandler.prototype.OnMouseWheel = function (event) {
        return false;
    };
    CAnnot_MousePtHandler.prototype.OnMouseOver = function (event) {
        return false;
    };
    CAnnot_MousePtHandler.prototype.OnMouseOut = function (event) {
        //         this.m_nFocusAnnot.nPage = -1;
        //         this.m_nFocusAnnot.nAnnotIndex = -1;
        //          this.m_bMoveDown = false;
        return false;
    };
    CAnnot_MousePtHandler.prototype.OnMouseLeave = function (event) {
        this.m_bMoveDown = false;
        this.m_pAnnotPlugin.OnDraw();
        return false;
    };
    CAnnot_MousePtHandler.prototype.OnMouseEnter = function (event) {
        return false;
    };

    FXREADER.Event.CAnnot_MousePtHandler = CAnnot_MousePtHandler;
    return FXREADER.Event.CAnnot_MousePtHandler;
});